package com.hkts.reportform.datastatics.serviceImpl;

import com.hkts.reportform.datastatics.dao.ReportDao;
import com.hkts.reportform.datastatics.entity.ReportForm;
import com.hkts.reportform.datastatics.service.GreenBodiesService;
import com.hkts.reportform.utils.DateUtil;
import com.hkts.reportform.utils.ErpProperties;
import com.hkts.reportform.utils.InventoryClassFildes;
import com.hkts.reportform.utils.InventoryClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("greenBodiesServiceImpl")
public class GreenBodiesServiceImpl implements GreenBodiesService {
    @Autowired
    private ReportDao reportDaoImpl;
    @Autowired
    private RedisTemplate redisTemplate;
    //生坯库存实时数据（昨日,前日,分类）
    @Override
    public void greenBodiesData()throws Exception {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<ReportForm> greenBodies = reportDaoImpl.getStoredProcedure(dateFormat.format(DateUtil.getYesterday()), dateFormat.format(DateUtil.getYesterday()), InventoryClassFildes.GREEN_BODY);
            List<String> listName = new ArrayList<>();
            List<Float> listValue = new ArrayList<>();
            List<Float> listValueByBefore = new ArrayList<>();
            for (int i = 0; i < greenBodies.size(); i++) {
                listValue.add(greenBodies.get(i).getStockOnHand());
                listName.add(greenBodies.get(i).getStockName());
            }
            List<ReportForm> greenBodiesByBefore = reportDaoImpl.getStoredProcedure(dateFormat.format(DateUtil.getYesterday(2)), dateFormat.format(DateUtil.getYesterday(2)), InventoryClassFildes.GREEN_BODY);
            for (int i = 0; i < greenBodiesByBefore.size(); i++) {
                listValueByBefore.add(greenBodiesByBefore.get(i).getStockOnHand());
            }
            redisTemplate.delete(ErpProperties.GREEN_YESTER_DAY_NAME);
            redisTemplate.delete(ErpProperties.GREEN_YESTER_DAY);
            redisTemplate.delete(ErpProperties.GREEN_BEFORE_YESTER_DAY);
            redisTemplate.opsForList().leftPushAll(ErpProperties.GREEN_YESTER_DAY, listValue);
            redisTemplate.opsForList().leftPushAll(ErpProperties.GREEN_YESTER_DAY_NAME, listName);
            redisTemplate.opsForList().leftPushAll(ErpProperties.GREEN_BEFORE_YESTER_DAY, listValueByBefore);
    }
}
